﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LuckStar
{
    public partial class PrizeForm : Form
    {
        Lucky luck;
        string  prizeid;
        int count = 0;
        List<string> users;
        List<string> Curusers;
        Font font;
        public PrizeForm(Lucky _luck,string  _pid)
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            luck = _luck;
            prizeid = _pid;
            if (_luck == null)
                return;         
            timer1.Stop();
            font= new Font("黑体", 35);            
            ShowInfo();
        }
        private void ShowInfo()
        {
            btn_save.Visible = false;
            btn_start.Enabled = false;
            btn_stop.Enabled = false;         
            users = LuckAction.GetUnWinnersList(luck);
       
            foreach(Prize p in luck.PrizeList)
            {
                if (p.guid == prizeid)
                {
                    count = p.Count;
                    this.Text = p.Name + "抽奖";
                    lb_name.Text = p.Name;
                    lb_pname.Text = p.PrizeName;
                    lb_count.Text = p.Count + "个名额";
                    if (System.IO.File.Exists(p.Image))
                        pictureBox1.Load(p.Image);
                    if (p.WinnersList.Count >= p.Count)//已经完成的了
                    {
                        BindUsers(p.WinnersList);
                        lb_scale.Text = "";
                    }
                    else
                    {
                        btn_start.Enabled = true;
                        lb_scale.Text = "中奖率" + (count * 1.0 / users.Count * 100).ToString("f2") + "%";
                    }
                   
                }
            }
           

        }

        /// <summary>
        /// 绑定显示
        /// </summary>
        /// <param name="users"></param>
        private void BindUsers(List<string> users)
        {
            Label lb;
            panel1.Controls.Clear();
            foreach (string s in users)
            {
                lb = new Label();
                lb.Font = font;
                lb.Text = s;
                lb.ForeColor = Color.Red;
                lb.AutoSize = true;
                panel1.Controls.Add(lb);
            }           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Curusers = LuckAction.GetRandomUser(users, count);
            BindUsers(Curusers);
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            timer1.Start();
            btn_save.Visible = false;
            btn_start.Enabled = false;
            btn_stop.Enabled = true;
        }
        private void btn_stop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            btn_save.Visible = true;
            btn_start.Enabled = true;
            btn_stop.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
           luck= LuckAction.SaveLuck(luck, prizeid, Curusers);
           LuckAction.SaveData(luck);
           ShowInfo();

        }

       
    }
}
